// 这个文件将主表与申请表的数据进行对比

import store from '@/store'
import { baseInfo, dateInfo, organInfo, investInfo, riskInfo, consignerInfo, rateInfo, fundFileInfo } from './contrastData'

/*
property   对比属性
propertyChineName  文字标题
stepObjectName  步骤
*/
export function publicCompare(property, propertyChineName, stepObjectName) { // 对比对象属性
	// 组件初始化的时候禁止调用
	if (!store.state.fundStep.specialtTotalData.productFundApply && !store.state.fundStep.specialtTotalData.productFundInfo) {
		return
	}
	if (!isData(1, stepObjectName) && !isData(2, stepObjectName)) { // 当申请表和主表都没有数据
		return false
	}
	const productFundApply = store.state.fundStep.specialtTotalData.productFundApply
	const productFundInfo = store.state.fundStep.specialtTotalData.productFundInfo
	const applyData = productFundApply && productFundApply[stepObjectName] ? productFundApply[stepObjectName] : {}
	const infoData = productFundInfo && productFundInfo[stepObjectName] ? productFundInfo[stepObjectName] : {}
	const info = infoData[property] ? infoData[property] : ''
	const Apply = applyData[property] ? applyData[property] : ''
	if (info !== Apply) { // 当申请表与主表的属性值不相同时
		if (!store.state.fundStep.specialAttribute.includes(propertyChineName)) {
			store.state.fundStep.specialAttribute.push(propertyChineName)
			store.state.fundStep.specialCheckNum[stepObjectName].num += 1
		}
		return true
	}
}

// 判断主表和申请表数据是否为空
/*
type 1   主表
type 2  申请表
stepPertyName   步骤对象字段名称
*/
function isData(type, stepPertyName) {
	if (type === 1) {
		if (store.state.fundStep.specialtTotalData.productFundInfo) {
			return !!store.state.fundStep.specialtTotalData.productFundInfo[stepPertyName]
		} else {
			return false
		}
	} else if (type === 2) {
		if (store.state.fundStep.specialtTotalData.productFundApply) {
			return !!store.state.fundStep.specialtTotalData.productFundApply[stepPertyName]
		} else {
			return false
		}
	}
}

// 对比费率数组又没有变化
/*
type 费率类型
fundCode 基金代码
msg 文字标题
*/
export function feeRateCCompare(type, msg) {
	// 组件初始化的时候禁止调用
	if (!store.state.fundStep.specialtTotalData.productFundApply && !store.state.fundStep.specialtTotalData.productFundInfo) {
		return false
	} else if (!isData(2, 'rateInfo') && !isData(1, 'rateInfo')) { // 当申请表和主表没有数据
		return false
	}
	const productFundApply = store.state.fundStep.specialtTotalData.productFundApply
	const productFundInfo = store.state.fundStep.specialtTotalData.productFundInfo
	const applyData = productFundApply && productFundApply.rateInfo ? productFundApply.rateInfo : {}
	const infoData = productFundInfo && productFundInfo.rateInfo ? productFundInfo.rateInfo : {}
	const applyFeeRateList = applyData.feeRateInfoList && Array.isArray(applyData.feeRateInfoList) ? applyData.feeRateInfoList : []
	const infoFeeRateList = infoData.feeRateInfoList && Array.isArray(infoData.feeRateInfoList) ? infoData.feeRateInfoList : []
	const infoFee = infoFeeRateList.filter(item => item.feeRateType === type)
	const ApplyFee = applyFeeRateList.filter(item => item.feeRateType === type)
	if (!infoFee.length && !ApplyFee.length) {
		return false
	} else if (infoFee.length !== ApplyFee.length) { // 若两者长度不一，直接返回
		if (!store.state.fundStep.specialAttribute.some(item => item === msg)) {
			store.state.fundStep.specialAttribute.push(msg)
			store.state.fundStep.specialCheckNum.rateInfo.num += 1
		}
		return true
	} else if (infoFee.length === ApplyFee.length) {
		if (['7', '8', '9'].includes(type)) {
			if (msg.includes('备注')) { // 对比费率备注
				const infoRemak = infoFee[0].remark ? infoFee[0].remark : ''
				const applyRemak = ApplyFee[0].remark ? ApplyFee[0].remark : ''
				if (infoRemak !== applyRemak) {
					if (!store.state.fundStep.specialAttribute.some(item => item === msg)) {
						store.state.fundStep.specialAttribute.push(msg)
						store.state.fundStep.specialCheckNum.rateInfo.num += 1
					}
					return true
				}
			} else if (msg.includes('算法')) { // 对比费率算法
				const infosalesServiceFeeAlgorithm = infoFee[0].salesServiceFeeAlgorithm ? infoFee[0].salesServiceFeeAlgorithm : ''
				const applysalesServiceFeeAlgorithm = ApplyFee[0].salesServiceFeeAlgorithm ? ApplyFee[0].salesServiceFeeAlgorithm : ''
				if (infosalesServiceFeeAlgorithm !== applysalesServiceFeeAlgorithm) {
					if (!store.state.fundStep.specialAttribute.some(item => item === msg)) {
						store.state.fundStep.specialAttribute.push(msg)
						store.state.fundStep.specialCheckNum.rateInfo.num += 1
					}
					return true
				}
			} else { // 对比费率
				const infofeeRate = infoFee[0].feeRate ? infoFee[0].feeRate : ''
				const applyfeeRate = ApplyFee[0].feeRate ? ApplyFee[0].feeRate : ''
				const infofeeRateUnit = infoFee[0].feeRateUnit ? infoFee[0].feeRateUnit : ''
				const applyfeeRateUnit = ApplyFee[0].feeRateUnit ? ApplyFee[0].feeRateUnit : ''
				if (infofeeRate !== applyfeeRate || infofeeRateUnit !== applyfeeRateUnit) {
					if (!store.state.fundStep.specialAttribute.some(item => item === msg)) {
						store.state.fundStep.specialAttribute.push(msg)
						store.state.fundStep.specialCheckNum.rateInfo.num += 1
					}
					return true
				}
			}
		} else {
			for (let index = 0; index < ApplyFee.length; index++) { // 对比长列表费率
				const keys = Object.keys(ApplyFee[index])
				let feeFateFlag = false
				keys.forEach(item => {
					const applyMsg = ApplyFee[index][item] ? ApplyFee[index][item] : ''
					const infoMsg = infoFee[index][item] ? infoFee[index][item] : ''
					if (applyMsg !== infoMsg) {
						feeFateFlag = true
					}
				})
				if (feeFateFlag) {
					if (!store.state.fundStep.specialAttribute.some(item => item === msg)) {
						store.state.fundStep.specialAttribute.push(msg)
						store.state.fundStep.specialCheckNum.rateInfo.num += 1
					}
					return true
				}
			}
		}
	}
}

// 对比基金经理信息
export function fundCompare() {
	// 组件初始化的时候禁止调用
	if (!store.state.fundStep.specialtTotalData.productFundApply && !store.state.fundStep.specialtTotalData.productFundInfo) {
		return false
	}
	const productFundApply = store.state.fundStep.specialtTotalData
	const productFundInfo = store.state.fundStep.specialtTotalData
	const applyList = productFundApply && productFundApply.fundManagerApplyList ? productFundApply.fundManagerApplyList : []
	const infoList = productFundInfo && productFundInfo.fundManagerInfoList ? productFundInfo.fundManagerInfoList : []

	// 当申请表和主表都没有数据
	if (!applyList.length && !infoList.length) {
		return false
	} else if (applyList.length !== infoList.length) { // 如果二者的长度不一样，则直接返回
		if (!store.state.fundStep.specialAttribute.includes('基金经理信息')) {
			store.state.fundStep.specialAttribute.push('基金经理信息')
			store.state.fundStep.specialCheckNum.fundMange.num += 1
		}
		return true
	} else if (applyList.length === infoList.length) {
		const propertyList = ['managerId', 'startDate', 'endDate', 'entryNoticeDate', 'leaveNoticeDate']// 如相同位置的数据不一样，直接返回
		for (let index = 0; index < applyList.length; index++) {
			const applyItem = applyList[index]
			const infoItem = infoList[index]
			for (let i = 0; i < propertyList.length; i++) {
				const property = propertyList[i]
				const applyItemProperty = applyItem[property] ? applyItem[property] : ''
				const infoItemProperty = infoItem[property] ? infoItem[property] : ''
				if (applyItemProperty !== infoItemProperty) {
					if (!store.state.fundStep.specialAttribute.includes('基金经理信息')) {
						store.state.fundStep.specialAttribute.push('基金经理信息')
						store.state.fundStep.specialCheckNum.fundMange.num += 1
					}
					return true
				}
			}
		}
	}
}

// 对比文件信息
// type 文件类型
// msg 文字标题
export function fileCompare(type, msg) {
	// 组件初始化的时候禁止调用
	if (!store.state.fundStep.specialtTotalData.productFundApply && !store.state.fundStep.specialtTotalData.productFundInfo) {
		return false
	}
	// 当申请表和主表都没有数据
	if (!isData(1, 'fundFileInfo') && !isData(2, 'fundFileInfo')) {
		return false
	}
	const productFundApply = store.state.fundStep.specialtTotalData.productFundApply
	const productFundInfo = store.state.fundStep.specialtTotalData.productFundInfo
	const applyData = productFundApply && productFundApply.fundFileInfo ? productFundApply.fundFileInfo : {}
	const infoData = productFundInfo && productFundInfo.fundFileInfo ? productFundInfo.fundFileInfo : {}
	const applyKeys = applyData[type] ? Object.keys(applyData[type]) : []
	const infoKeys = infoData[type] ? Object.keys(infoData[type]) : []
	if (!applyKeys.length && !infoKeys.length) {
		return false
	} else if (applyKeys.length || infoKeys.length) {
		const lengthFlag = applyKeys.length < infoKeys.length
		const list = lengthFlag ? infoKeys : applyKeys
		for (let index = 0; index < list.length; index++) {
			// 防止空属性作为对象
			const infoObj = infoData[type] ? infoData[type] : {}
			const applyObj = applyData[type] ? applyData[type] : {}
			const infoProperty = infoObj[list[index]] ? infoObj[list[index]] : ''
			const applyProperty = applyObj[list[index]] ? applyObj[list[index]] : ''
			if (infoProperty !== applyProperty) {
				if (!store.state.fundStep.specialAttribute.includes(msg)) {
					store.state.fundStep.specialAttribute.push(msg)
					store.state.fundStep.specialCheckNum.fileInfo.num += 1
				}
				return true
			}
		}
	}
}

// 变更保存时将页面数据与主表数据做对比
export function updateCompareSubmit(comShow) {
	baseInfo.forEach(item => { /* 1.基本信息对比*/
		publicCompare(item.key, item.value, 'baseInfo')
	})
	dateInfo.forEach(item => { /* 2.对比日期*/
		publicCompare(item.key, item.value, 'dateInfo')
	})
	organInfo.forEach(item => { /* 3.对比机构信息*/
		publicCompare(item.key, item.value, 'organInfo')
	})
	investInfo.forEach(item => { /* 5.对比投资相关信息*/
		publicCompare(item.key, item.value, 'investInfo')
	})
	riskInfo.forEach(item => { /* 6.对比风控信息*/
		publicCompare(item.key, item.value, 'riskInfo')
	})
	if (comShow) { // 当基础信息-计划类型为单项资产管理时
		consignerInfo.forEach(item => { /* 对比委托人信息*/
			publicCompare(item.key, item.value, 'consignerInfo')
		})
	}
	fundCompare()// 8.对比基金经理
	fundFileInfo.forEach(item => { // 9.对比文件信息
		item.typeName.forEach(items => {
			fileCompare(items.objName, `${item.type}-${items.title}`)
		})
	})
	rateInfo.forEach(item => { // 4.对比费率
		if (!item.children) {
			publicCompare(item.key, item.value, 'rateInfo')
		} else {
			item.children.forEach(items => { // 对比费率列表
				feeRateCCompare(items.feeRateType, items.msg)
			})
		}
	})
}

